//
//  WBTitleButton.swift
//  TYCWeiBo
//
//  Created by 中发 on 2018/6/7.
//  Copyright © 2018年 中发. All rights reserved.
//

import UIKit

class WBTitleButton: UIButton {

    /// 重载构造函数
    ///
    /// - Parameter title: title如果为nil，就显示首页，如果不为nil，就显示title和箭头图像
    init(title: String?) {
        super.init(frame: CGRect())
        
        if title == nil {
            setTitle("首页", for: .normal)
        }else {
            setTitle(title, for: .normal)
            //设置图像
            setImage(UIImage(named: ""), for: .normal)
            setImage(UIImage(named: ""), for: .selected)
        }
        //设置字体和颜色
        titleLabel?.font = UIFont.boldSystemFont(ofSize: 17)
        setTitleColor(UIColor.darkGray, for: .normal)
        //设置大小
        sizeToFit()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        //判断titleLabel和imageView是否存在
        guard let titleLabel = titleLabel, let imageView = imageView else { return }
        //将titleLabel的x向左移动imageView的宽度
        //OC中不允许直接修改结构体内部的值
        //Swift可以直接修改
        titleLabel.frame.origin.x = 0
//        titleLabel.frame = titleLabel.frame.insetBy(dx: -imageView.bounds.width, dy: 0)
        //将imageView的x向右移动titleLabel的宽度
//        imageView.frame = imageView.frame.insetBy(dx: titleLabel.bounds.width, dy: 0)
        imageView.frame.origin.x = titleLabel.bounds.width
    }
}
